<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>菜单管理管理</title>
<meta name="decorator" content="default"/>
<script type="text/javascript">

	//url
	var listUrl = '${ctx}/sys/menu/listMenu';
	var cardUrl = '${ctx}/sys/menu/formMenu';

	//按钮状态  {状态值为 0 不可用 ；1 可用}
	var buttonStatus = {};
	buttonStatus.add 	= {add:{id:'btnAdd',st:0},edit:{id:'btnEdit',st:0},save:{id:'btnSubmit',st:1},cancel:{id:'btnCancel',st:1},back:{id:'btnBack',st:1}};
	buttonStatus.edit	= {add:{id:'btnAdd',st:0},edit:{id:'btnEdit',st:0},save:{id:'btnSubmit',st:1},cancel:{id:'btnCancel',st:1},back:{id:'btnBack',st:1}};
	buttonStatus.save   = {add:{id:'btnAdd',st:1},edit:{id:'btnEdit',st:1},save:{id:'btnSubmit',st:0},cancel:{id:'btnCancel',st:0},back:{id:'btnBack',st:1}};
	buttonStatus.cancel = {add:{id:'btnAdd',st:1},edit:{id:'btnEdit',st:"${not empty menu.id?'1':'0'}"},save:{id:'btnSubmit',st:0},cancel:{id:'btnCancel',st:0},back:{id:'btnBack',st:1}}
	buttonStatus.other  = {add:{id:'btnAdd',st:1},edit:{id:'btnEdit',st:"${not empty menu.id?'1':'0'}"},save:{id:'btnSubmit',st:0},cancel:{id:'btnCancel',st:0},back:{id:'btnBack',st:1}};

	$(document).ready(function() {
		$("#inputForm").validate({
			submitHandler: function(form){
				loading('正在提交，请稍等...');
				form.submit();
			},
			errorContainer: "#messageBox",
			errorPlacement: function(error, element) {
				$("#messageBox").text("输入有误，请先更正。");
				if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
					error.appendTo(element.parent().parent());
				} else {
					error.insertAfter(element);
				}
			}
		});
		updatePageStatus();
		initParentNode();
	});
	
	function initParentNode(){ //初始化上级菜单
		var formIdValue = jQuery('[name="id"]').val();
		var parentInputObj = jQuery('[name="parent.id"]');
		if(formIdValue== null || formIdValue == ''){
			var parentId = parentInputObj.val();
			if((parentId == null || parentId == '') && parent.getSelectNode!=null){
				var selectNode = parent.getSelectNode()[0];
				if(selectNode!=null){
					parentInputObj.val(selectNode.id);
					jQuery('[name="parent.name"]').val(selectNode.name);
				}
			}
		}
	}
	
	function updatePageStatus(){
		var currentOpt = "${opt}";
		if(currentOpt==null||currentOpt==''){
			currentOpt = 'other';
		}
		var btnStatus = buttonStatus[currentOpt];
		freshButtonStatus(btnStatus);
		if(currentOpt=='add' || currentOpt=='edit'){
			setAllPageEnable(jQuery("#formAreaId"));
		}else{
			setAllPageDisable(jQuery("#formAreaId"));
		}
		if('save' == currentOpt){
			 parent.refreshTree();
		}
		if('${message}'!=''){
			 alertx('${message}');
		}
	}
	function add(){ //新增记录
		var url = cardUrl+"?opt=add";
		url = addUrlParam(url,'parent.id',jQuery('[name="parent.id"]').val());
		window.location.href = url;
	}
	
	function edit(){ //修改记录
		var btnStatus = buttonStatus['edit'];
		freshButtonStatus(btnStatus);
		setAllPageEnable(jQuery("#formAreaId"));
	}
	
	function cancel(){ //取消操作
		var btnStatus = buttonStatus['cancel'];
		freshButtonStatus(btnStatus);
		setAllPageDisable(jQuery("#formAreaId"));
		jQuery('#inputForm')[0].reset();
		jQuery("#formAreaId").find(':input').each(function(){ 
			if(this.type.indexOf('select')>-1){
            	jQuery(this).select2();   //jQuery(this).select2();
            }
		});
	}
	
	function back(){ //返回
		var btnStatus = buttonStatus['other'];
		freshButtonStatus(btnStatus);
		setAllPageDisable(jQuery("#formAreaId"));
		parent.queryListPage();
	}
	
	function save(){ //保存操作
		var formObj = jQuery('#inputForm');
		var res = formObj.valid();
		if(res == true){
			
			formObj[0].submit();
		}
	}
</script>
</head>
<body>
	<form:form id="inputForm" modelAttribute="menu" action="${ctx}/sys/menu/saveMenu" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>	
		
		<div class="breadcrumb form-search" id="toolButtonId">
			<shiro:hasPermission name="sys:menu:edit">&nbsp;
				<input id="btnAdd" class="btn btn-primary" type="button" value="新 增" onclick="add()"/>&nbsp;
				<input id="btnEdit" class="btn btn-primary" type="button" value="修 改" onclick="edit()"/>&nbsp;
				<input id="btnSubmit" class="btn btn-primary" type="button" value="保 存" onclick="save()"/>&nbsp;
				<input id="btnCancel" class="btn" type="button" value="取 消" onclick="cancel()"/>
			</shiro:hasPermission>
			<input id="btnBack" class="btn" type="button" value="返 回" onclick="back()"/>
		</div>
		
		<div id="formAreaId">	
			<div class="control-group">
				<label class="control-label">上级菜单：</label>
				<div class="controls">
				<sys:treeselect id="parent" name="parent.id" value="${menu.parent.id}" labelName="parent.name" labelValue="${fns:getMenu(menu.parent.id).name}"
					title="上级菜单" url="/sys/menu/treeData" cssClass="input-small" allowClear="true" notAllowSelectParent="false"/>
					<span class="help-inline"><font color="red">*</font> </span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">名称:</label>
				<div class="controls">
					<form:input path="name" htmlEscape="false" maxlength="50" class="required input-xlarge"/>
					<span class="help-inline"><font color="red">*</font> </span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">链接:</label>
				<div class="controls">
					<form:input path="href" htmlEscape="false" maxlength="2000" class="input-xxlarge"/>
					<span class="help-inline">点击菜单跳转的页面</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">目标:</label>
				<div class="controls">
					<form:input path="target" htmlEscape="false" maxlength="10" class="input-small"/>
					<span class="help-inline">链接地址打开的目标窗口，默认：mainFrame</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">图标:</label>
				<div class="controls">
					<sys:iconselect id="icon" name="icon" value="${menu.icon}"/>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">排序：</label>
				<div class="controls">
					<form:input path="sort" htmlEscape="false" class="input-xlarge required digits"/>
					<span class="help-inline"><font color="red">*</font> </span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">可见:</label>
				<div class="controls">
					<form:radiobuttons path="isShow" items="${fns:getDictList('show_hide')}" itemLabel="label" itemValue="value" htmlEscape="false" class="required"/>
					<span class="help-inline">该菜单或操作是否显示到系统菜单中</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">权限标识:</label>
				<div class="controls">
					<form:input path="permission" htmlEscape="false" maxlength="100" class="input-xxlarge"/>
					<span class="help-inline">控制器中定义的权限标识，如：@RequiresPermissions("权限标识")</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">备注:</label>
				<div class="controls">
					<form:textarea path="remarks" htmlEscape="false" rows="3" maxlength="200" class="input-xxlarge"/>
				</div>
			</div>
		</div>
	</form:form>
	<div style="height: 50px;width: 100%;"></div>
</body>
</html>